//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ConstantParameter.h"
using namespace Cephei::QL::Models;
#include <gen/QL/Math/Optimization/Constraint.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/Parameter.h>
using namespace Cephei::QL::Math::Optimization;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::CConstantParameter::CConstantParameter (Double value, Cephei::QL::Math::Optimization::IConstraint^ constraint) : CParameter(CConstantParameter::typeid)
{
    CConstraint^ _Cconstraint;
    try
    {
#ifdef HANDLE
        _phConstantParameter = NULL;
#endif
        QuantLib::Real _value = (QuantLib::Real)ValueHelper::Convert (value); //d
        _Cconstraint = safe_cast<CConstraint^> (constraint);
        _Cconstraint->Lock();
        QuantLib::Constraint& _constraint = static_cast<QuantLib::Constraint&> (_Cconstraint->GetReference ()); 
        _ppConstantParameter = new boost::shared_ptr<QuantLib::ConstantParameter> (new QuantLib::ConstantParameter ( _value,  _constraint ));
        SetParameter (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppConstantParameter));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cconstraint != nullptr) _Cconstraint->Unlock();
    }
}
Cephei::QL::Models::CConstantParameter::CConstantParameter (Cephei::QL::Math::Optimization::IConstraint^ constraint) : CParameter(CConstantParameter::typeid)
{
    CConstraint^ _Cconstraint;
    try
    {
#ifdef HANDLE
        _phConstantParameter = NULL;
#endif
        _Cconstraint = safe_cast<CConstraint^> (constraint);
        _Cconstraint->Lock();
        QuantLib::Constraint& _constraint = static_cast<QuantLib::Constraint&> (_Cconstraint->GetReference ()); 
        _ppConstantParameter = new boost::shared_ptr<QuantLib::ConstantParameter> (new QuantLib::ConstantParameter ( _constraint ));
        SetParameter (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppConstantParameter));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cconstraint != nullptr) _Cconstraint->Unlock();
    }
}
Cephei::QL::Models::CConstantParameter::CConstantParameter (boost::shared_ptr<QuantLib::ConstantParameter>& childNative, Object^ owner) : CParameter(CConstantParameter::typeid)
{
#ifdef HANDLE
	_phConstantParameter = NULL;
#endif
	_ppConstantParameter = &childNative;
    _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppConstantParameter));
}
Cephei::QL::Models::CConstantParameter::CConstantParameter (QuantLib::ConstantParameter& childNative, Object^ owner) : CParameter(CConstantParameter::typeid)
{
#ifdef HANDLE
	_phConstantParameter = NULL;
#endif
	_ppConstantParameter = new boost::shared_ptr<QuantLib::ConstantParameter> (&childNative);
    _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppConstantParameter));
    _ConstantParameterOwner = owner;
    _ParameterOwner = owner;
}

Cephei::QL::Models::CConstantParameter::CConstantParameter (CConstantParameter^ copy) : CParameter(CConstantParameter::typeid)
{
#ifdef HANDLE
	_phConstantParameter = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppConstantParameter = new boost::shared_ptr<QuantLib::ConstantParameter> (copy->GetShared());
        _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppConstantParameter));
    }
}
Cephei::QL::Models::CConstantParameter::CConstantParameter (PLATFORM::Type^ t) : CParameter(CConstantParameter::typeid)
{
#ifdef HANDLE
	_phConstantParameter = NULL;
#endif
	if (!t->IsSubclassOf(CConstantParameter::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::CConstantParameter::CConstantParameter (QuantLib::Handle<QuantLib::ConstantParameter>& childNative, Object^ owner)  : CParameter(CConstantParameter::typeid)
{
	_phConstantParameter = &childNative;
	_ppConstantParameter = &static_cast<boost::shared_ptr<QuantLib::ConstantParameter>>(childNative.currentLink());
    _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppConstantParameter));
    _ConstantParameterOwner = owner;
}
Cephei::QL::Models::CConstantParameter::CConstantParameter (QuantLib::Handle<QuantLib::ConstantParameter> childNative)  : CParameter(CConstantParameter::typeid)
{
	_phConstantParameter = &childNative;
	_ppConstantParameter = &static_cast<boost::shared_ptr<QuantLib::ConstantParameter>>(childNative.currentLink());
    _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppConstantParameter));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::CConstantParameter::CConstantParameter (QuantLib::ConstantParameter childNative)  : CParameter(CConstantParameter::typeid)
{
#ifdef HANDLE
	_phConstantParameter = NULL;
#endif
	_ppConstantParameter = new boost::shared_ptr<QuantLib::ConstantParameter> (new QuantLib::ConstantParameter (childNative));
    _ppParameter = new boost::shared_ptr<QuantLib::Parameter> (boost::dynamic_pointer_cast<QuantLib::Parameter> (*_ppConstantParameter));
}
#endif

Cephei::QL::Models::CConstantParameter::~CConstantParameter ()
{
    if (_ppConstantParameter != NULL)
    {
	    delete _ppConstantParameter;
        _ppConstantParameter = NULL;
    }
}
Cephei::QL::Models::CConstantParameter::!CConstantParameter ()
{
    if (_ppConstantParameter != NULL)
    {
	    delete _ppConstantParameter;
    }
}
QuantLib::ConstantParameter& Cephei::QL::Models::CConstantParameter::GetReference ()
{
    if (_ppConstantParameter == NULL) throw REFNEW NativeNullException ();
	return **_ppConstantParameter;
}
boost::shared_ptr<QuantLib::ConstantParameter>& Cephei::QL::Models::CConstantParameter::GetShared ()
{
    if (_ppConstantParameter == NULL) throw REFNEW NativeNullException ();
	return *_ppConstantParameter;
}
QuantLib::ConstantParameter* Cephei::QL::Models::CConstantParameter::GetPointer ()
{
    if (_ppConstantParameter == NULL) throw REFNEW NativeNullException ();
	return &**_ppConstantParameter;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ConstantParameter>& Cephei::QL::Models::CConstantParameter::GetHandle ()
{
	if (_phConstantParameter == NULL)
	{
		_phConstantParameter = new Handle<QuantLib::ConstantParameter> (*_ppConstantParameter);
	}
	return *_phConstantParameter;
}
#endif
bool Cephei::QL::Models::CConstantParameter::HasNative () 
{
	return (_ppConstantParameter != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::IConstantParameter^ Cephei::QL::Models::CConstantParameter_Factory::Create (Double value, Cephei::QL::Math::Optimization::IConstraint^ constraint)
{
    return REFNEW CConstantParameter ( value,  constraint);
}
Cephei::QL::Models::IConstantParameter^ Cephei::QL::Models::CConstantParameter_Factory::Create (Cephei::QL::Math::Optimization::IConstraint^ constraint)
{
    return REFNEW CConstantParameter ( constraint);
}
